package service.mailer;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.ejb.Asynchronous;
import javax.mail.*;
import javax.mail.internet.*;

/**
 *
 * @author Juan Sebastian Hernández Serrato
 */
public class UPNMail {
    
    public enum Protocol {
        SMTP,
        SMTPS,
        TLS
    }
    
    private int port = 25;
    private String host = "correoupn.pedagogica.edu.co";
    private String from = "informacionupn@pedagogica.edu.co";
    private Protocol protocol = Protocol.SMTP;
    private boolean debug = false;
    
    
    private String to;//="jshernandezs@pedagogica.edu.co";
    private String textContent;// = "Hi, Nice to meet you!";
    private String htmlContent;// = "<html><h1>Hi</h1><p>Nice to meet you!</p></html>";
    private String subject;//="Prueba de mail";

    
    @Asynchronous
    public void sendEmail() throws UnsupportedEncodingException, AddressException, MessagingException{

        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        switch (protocol) {
            case SMTPS:
                props.put("mail.smtp.ssl.enable", true);
                break;
            case TLS:
                props.put("mail.smtp.starttls.enable", true);
                break;
        }

        Session session = Session.getInstance(props);
        session.setDebug(debug);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(getTo()));
        message.setSubject(getSubject());
        message.setSentDate(new Date());
        
        Multipart multipart = new MimeMultipart("alternative");
        
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setText(getTextContent());
        
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(getHtmlContent(), "text/html");
        
        multipart.addBodyPart(textPart);
        multipart.addBodyPart(htmlPart);
        message.setContent(multipart);
        
        Transport.send(message);
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getTextContent() {
        return textContent;
    }

    public void setTextContent(String textContent) {
        this.textContent = textContent;
    }

    public String getHtmlContent() {
        return htmlContent;
    }

    public void setHtmlContent(String htmlContent) {
        this.htmlContent = htmlContent;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }
}
